<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class table_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();
		$this->CI->load->model('Folder_model');
		$this->CI->load->model('Table1_model');
		$this->CI->load->model('Table2_model');
		$this->CI->load->model('Table3_model');
		$this->CI->load->model('Table4_model');
		$this->CI->load->model('Table5_model');
		$this->CI->load->model('Table5_student_model');
		$this->CI->load->model('Table6_model');
		$this->CI->load->library('PHPWord/PHPWord');
	}
	
	/**  
	* 根据id获取文件夹
	* @access public 
	* @param  $id 文件夹id
	* @return array
	*/  
	function getFolderById($id){
		$folder=$this->CI->Folder_model->get_single(array('id'=>$id));
		return $folder;
	}

	/**  
	* 根据表类型取出文件夹
	* @access public 
	* @param  $type 类型 ，默认不删除
	* @param  $admin_id s所属人
	* @param  $del 是否删除 ，默认不删除
	* @param  $role 角色，默认只能看自己
	* @return array
	*/  
	public function getFolderByType($type,$admin_id,$del=false,$role=false){
		// if($role){
		// 	if($role==WebRole::SupervisorDirector[XPHEnum::Value] || $role==WebRole::SchoolSupervisor[XPHEnum::Value]){
		// 		$cond=array('type'=>$type,'admin_id'=>$admin_id);
		// 	}else if($role==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){
		// 		$cond=array('type'=>$type,'');
		// 	}			
		// }else{
		// 	$cond=array('type'=>$type,'admin_id'=>$admin_id);
		// }		
		// if(!$del){
		// 	$cond['status']=CommonStatus::UnDeleted[XPHEnum::Value];
		// }
		// $folder=$this->CI->Folder_model->get_list_($cond);
		$connar['where']='1=1';
		$connar['items']='db_folder.*';
		$connar['join']=array('db_admin','db_admin.id=db_folder.admin_id');
		if($role){
			if($role==WebRole::SupervisorDirector[XPHEnum::Value] || $role==WebRole::SchoolSupervisor[XPHEnum::Value] || $role==WebRole::UnitSupervisor[XPHEnum::Value]){
				$connar['where'].=$this->CI->sqlEscape(' and db_folder.type=?? and db_folder.admin_id=??',array($type,$admin_id));
			}else if($role==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){
				$connar['where'].=$this->CI->sqlEscape(' and db_folder.type=?? and db_admin.dept_id=??',array($type,getSess()['dept_id']));
			}else if($role==WebRole::Manage[XPHEnum::Value] ||$role==WebRole::Admin[XPHEnum::Value]){
				$connar['where'].=$this->CI->sqlEscape(' and db_folder.type=?? ',array($type));
			}		
		}else{
			$connar['where'].=$this->CI->sqlEscape(' and db_folder.type=?? and db_folder.admin_id=??',array($type,$admin_id));
		}		
		if(!$del){
			$connar['where'].=$this->CI->sqlEscape(' and db_folder.status=??',array(CommonStatus::UnDeleted[XPHEnum::Value]));
		}
		$folder=$this->CI->Folder_model->get_list_full($connar);
		foreach ($folder as $k => $v) {
			if($v['is_have']==CommonWhether::Whether[XPHEnum::Value]){
				$folder[$k]['img']=base_url().'public/home/images/have_folder.png';
			}
			else{
				$folder[$k]['img']=base_url().'public/home/images/no_folder.png';
			}
		}
		return $folder;
	}

	/**  
	* 新增文件夹
	* @access public 
	* @param  $type 类型 ，默认不删除
	* @param  $admin_id s所属人
	* @param  $name 文件夹名
	* @return array
	*/  
	public function addFolder($type,$admin_id,$name){
		$add=array(
			'admin_id'=>$admin_id,
			'type'=>$type,
			'name'=>$name,
			'status'=>CommonStatus::UnDeleted[XPHEnum::Value],
			'is_have'=>CommonWhether::Not[XPHEnum::Value],
			'created'=>time()
			);
		$id=$this->CI->Folder_model->add($add);
		$add['id']=$id;
		$add['img']=base_url().'public/home/images/no_folder.png';
		return $add;
	}


	/**  
	* 删除文件夹
	* @access public 
	* @param  $id 文件夹id
	* @return array
	*/  
	public function delFolder($id){
		$this->CI->Folder_model->update(array('id'=>$id,'status'=>CommonStatus::Deleted[XPHEnum::Value]));
	}
	
	
	/**  
	* 修改文件夹名称
	* @access public 
	* @param  $id 文件夹id
	* @param  $name 文件夹名
	* @return array
	*/  
	public function resetName($id,$name){
		$update=array(
			'id'=>$id,
			'name'=>$name
			);
		$this->CI->Folder_model->update($update);
		$folder=$this->getFolderById($id);
		return $folder;
	}

	/**  
	* 获取文件夹下所有文件
	* @access public 
	* @param  $folder_id 文件夹id
	* @return array
	*/  
	public function getDocumentByFolderId($folder_id){
		$folder=$this->getFolderById($folder_id);
		$model=(new OtherTableType)->showByIndex($folder['type'],XPHEnum::Remark).'_model';		
		$document=$this->CI->$model->get_list(array('admin_id'=>$folder['admin_id'],'folder_id'=>$folder_id,'status'=>CommonStatus::UnDeleted[XPHEnum::Value]));
		if($document){
			return $document;
		}
		else{
			return array();
		}
	}

	/**  
	* 根据id和type获取文件
	* @access public 
	* @param  $id 文件id
	* @param  $type 文件类型
	* @return array
	*/  
	function getDocumentByIdAndType($id,$type){
		$model=(new OtherTableType)->showByIndex($type,XPHEnum::Remark).'_model';
		$document=$this->CI->$model->get_single(array('id'=>$id));
		return $document;
	}

	/**  
	* 修改文件名称
	* @access public 
	* @param  $id 文件id
	* @param  $name 文件名
	* @param  $type 表
	* @return array
	*/  
	public function resetDocumentName($id,$name,$type){
		$update=array(
			'id'=>$id,
			'name'=>$name
			);
		$model=(new OtherTableType)->showByIndex($type,XPHEnum::Remark).'_model';
		$this->CI->$model->update($update);
		$document=$this->getDocumentByIdAndType($id,$type);
		return $document;
	}

	/**  
	* 删除文件
	* @access public 
	* @param  $id 文件id
	* @return array
	*/  
	public function delDocument($id,$type){
		$model=(new OtherTableType)->showByIndex($type,XPHEnum::Remark).'_model';
		$this->CI->$model->update(array('id'=>$id,'status'=>CommonStatus::Deleted[XPHEnum::Value]));
		// $document=$this->getDocumentByIdAndType($id,$type);
		//如果文件夹下不存在文件了，那么更新文件夹的ishave为0
		// $folder_document=$this->getDocumentByFolderId($document['folder_id']);
		// if(count($folder_document)==0){
		// 	$this->CI->Folder_model->update(array('id'=>$document['folder_id'],'is_have'=>CommonWhether::Not[XPHEnum::Value]));
		// }
	}

	/**  
	* 判断是否文件夹下还有文件
	* @access public 
	* @param  $id 文件id
	* @return array
	*/  
	public function getFolderImg($id,$type){
		$document=$this->getDocumentByIdAndType($id,$type);
		//如果文件夹下不存在文件了，那么更新文件夹的ishave为0
		$folder_document=$this->getDocumentByFolderId($document['folder_id']);
		if(count($folder_document)==0){
			return base_url().'public/home/images/no_folder.png';
		}
		return base_url().'public/home/images/have_folder.png';
	}


	/**  
	* 获取table5 下学生
	* @access public 
	* @param  $table5_id table5id
	* @return array
	*/  
	public function getTable5Student($table5_id){
		$student=$this->CI->Table5_student_model->get_list(array('table5_Id'=>$table5_id));
		if(!$student){
			$student=array(0=>array('student_class_name'=>'','student_name'=>'','contact_phone'=>''));
		}
		return $student;
	}


	/**  
	* 导出table1
	* @access public 
	* @param  $table_id table1id
	* @return array
	*/  
	public function tableExport1($table_id){
		$table=$this->CI->Table1_model->get_single(array('id'=>$table_id));
		// var_dump($table);
		// exit();

		$PHPWord = new PHPWord();  
		  
		// New portrait section  
		$section = $PHPWord->createSection();  
		$PHPWord->addFontStyle('rStyle', array('bold'=>true,'color'=>'000000','size'=>16));  
		$PHPWord->addParagraphStyle('pStyle', array('align'=>'center'));  
		$section->addText(toText(OtherTableType::Table1[XPHEnum::Caption]), 'rStyle', 'pStyle');  

		$styleTablet = array('cellMargin'=>80);  
		$fontStylet = array('align'=>'center','bold'=>true);  
		$cellStylet = array('borderColor'=>'000000');  
		// Add table style  
		$PHPWord->addTableStyle('myOwnTableStylet', $styleTablet);  
		$tablet = $section->addTable('myOwnTableStylet');  		
		$tablet->addRow();  
		$tablet->addCell(5000,$cellStylet)->addText(toText('调查题目：'.$table['title']),$fontStylet);
		  

        $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $director = "upload/export/" . date('Ymd', time()) . "/";
        if (!file_exists($director)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($director, 0777);
        }
       	$file_name =OtherTableType::Table1[XPHEnum::Value].buildOrdeNo().'.docx';
       	
       	$objWriter->save($director.iconv('utf-8', 'gb2312', $file_name));  
       	return $director.$file_name;
	}


	/**  
	* 获取文件夹下所有文件
	* @access public 
	* @param  $folder_id 文件夹id
	* @return array
	*/  
	public function getDocumentList($type){
		$model=(new OtherTableType)->showByIndex($type,XPHEnum::Remark).'_model';	
		$db='db_'.strtolower((new OtherTableType)->showByIndex($type,XPHEnum::Remark));
		$connar['items']="db_admin.dept_id,db_admin.dept_name,db_admin.username,".$db.".*";
		$connar['join']=array('db_admin','db_admin.id='.$db.'.admin_id');
		$connar['where']=$this->CI->sqlEscape($db.'.status=??',array(CommonStatus::UnDeleted[XPHEnum::Value]));
		$this->CI->load->model("Admin_model");
		$role=explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);
		if(in_array(WebRole::Manage[XPHEnum::Value], $role)  || in_array(WebRole::Admin[XPHEnum::Value], $role) ){
			$connar['where'].=' and 1=1';
		}else if( in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
			$connar['where'].=$this->CI->sqlEscape(' and db_admin.dept_id=??',array(getSess()['dept_id']));
		}else if(in_array(WebRole::SupervisorDirector[XPHEnum::Value], $role)  || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role)  ||in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role)){
			$connar['where'].=$this->CI->sqlEscape(' and '.$db.'.admin_id=??',array(getSess()['id']));
		}
		
		$document=$this->CI->$model->get_list_full($connar);
		if($document){
			return $document;
		}
		else{
			return array();
		}
	}

	
}